package com.example.demo07cache.service;

import com.alicp.jetcache.anno.CacheType;
import com.alicp.jetcache.anno.Cached;
import com.example.demo07cache.dao.JetCacheDao;
import com.example.demo07cache.entity.Employee;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.concurrent.TimeUnit;

/**
 * @author wangxun
 */
@Service
public class JetCacheService {

    @Autowired
    private JetCacheDao jetCacheDao;

    /**
     * jetcache.areaInCacheName = false
     * --> RedisKey = redis:employee:getEmployeeById:{id}
     * jetcache.areaInCacheName = true
     * --> RedisKey = default_redis:employee:getEmployeeById:{id}
     * 注意:
     * 1. 默认不缓存 Null 值
     * 2. cacheType = CacheType.REMOTE，如果 Redis 中没有缓存，那么会重建缓存，重建缓存不会加分布式锁，如果并发比较大，可能会出现缓存击穿的情况
     */
    @Cached(name = "redis:employee:getEmployeeById:", key = "#id", expire = 10, timeUnit = TimeUnit.MINUTES, cacheType = CacheType.BOTH)
    public Employee getEmployeeById(Integer id) {
        return jetCacheDao.getEmployeeById(id);
    }
}
